package com.duing.util;

import com.duing.entity.IllnessData;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GsonUtil {

    public static void main(String[] args) {
        handleGraphData();
    }

    public static Map handleGraphData() {
        Map resultMap = new HashMap();

        String url = "https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=chinaDayList,chinaDayAddList";
        String result = HttpClientUtil.doGet(url);

        Gson gson = new Gson();
        Map map = gson.fromJson(result, Map.class);
        Map dataMap = (Map) map.get("data");

        ArrayList dataList = (ArrayList) dataMap.get("chinaDayAddList");
        // 存储日期 和 本土新增确诊人数
        ArrayList<String> xList = new ArrayList<String>();
        ArrayList<Integer> yList = new ArrayList<Integer>();
        for (int i = 0; i < dataList.size(); i++) {
            Map tmp = (Map) dataList.get(i);
            String date = (String) tmp.get("date");
            xList.add(date);

            double localConfirmAdd = (Double) tmp.get("localConfirmadd");
            yList.add((int) localConfirmAdd);
        }

        resultMap.put("xList", xList);
        resultMap.put("yList", yList);
        return resultMap;
    }


    public static List<IllnessData> handleData() {
        String url = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5";
        String result = HttpClientUtil.doGet(url);
        Gson gson = new Gson();
        Map map = gson.fromJson(result, Map.class);

        String subStr = (String) map.get("data");
        Map subMap = gson.fromJson(subStr, Map.class);

        ArrayList areaList = (ArrayList) subMap.get("areaTree");
        Map dataMap = (Map) areaList.get(0);

        ArrayList childrenList = (ArrayList) dataMap.get("children");

        List<IllnessData> resultList = new ArrayList<IllnessData>();
        for (int i = 0; i < childrenList.size(); i++) {
            Map tmp = (Map) childrenList.get(i);
            String area = (String) tmp.get("name");
            Map totalMap = (Map) tmp.get("total");
            double confirm = (Double) totalMap.get("confirm");
            double nowConfirm = (Double) totalMap.get("nowConfirm");
            double dead = (Double) totalMap.get("dead");
            double heal = (Double) totalMap.get("heal");

            IllnessData illnessData = new IllnessData(area,
                    (int) confirm, (int) nowConfirm, (int) heal, (int) dead);
            resultList.add(illnessData);
        }

        return resultList;
    }
}
